# -*- coding: utf-8 -*-
import scrapy,json,requests
from lxml import etree
from xiangmu.items import Zhilian


class ZhilianSpider(scrapy.Spider):
    name = 'zhilian'
    allowed_domains = ['zhaopin.com']
    start_urls = ['https://fe-api.zhaopin.com/c/i/sou?start=6900pageSize=60&cityId=489&workExperience=-1&education=-1&companyType=-1&employmentType=-1&jobWelfareTag=-1&kw=Python&kt=3&at=bf75702bfc484599bca08c812829e464&rt=27049133e96a48a390c54ad97b40fb18&_v=0.33934553&userCode=651235096&x-zp-page-request-id=0e6766ed75914d22a2f2f96099966ee5-1543886907926-734571']

    def parse(self, response):
        # with open('zhilian.html','wb') as f:
        #     f.write(response.body)
        # print(type(response),response)
        res_str = response.text
        res_dict = json.loads(res_str)
        d_list = res_dict['data']['results']
        for urls in d_list:
            url = urls['positionURL']
            yield scrapy.Request(url, callback=self.jobinfo)

            # print('向详情传入:'+url)
            # break
        # next = response.xpath('//a[@class="next page-numbers"]/@href').extract_first()
        xyy = 'https://fe-api.zhaopin.com/c/i/sou?start={}&pageSize=6660&cityId=489&workExperience=-1&education=-1&companyType=-1&employmentType=-1&jobWelfareTag=-1&kw=Python&kt=3&at=bf75702bfc484599bca08c812829e464&rt=27049133e96a48a390c54ad97b40fb18&_v=0.33934553&userCode=651235096&x-zp-page-request-id=0e6766ed75914d22a2f2f96099966ee5-1543886907926-734571'
        for i in range(2,530):
            next = xyy.format(i*60)
            yield scrapy.Request(next, callback=self.parse)
            # print(next)
    def jobinfo(self,response):
        # with open('zhilian.html', 'wb') as f:
        #     f.write(response.content)
        html_str = response.text
        # print(type(html_str),html_str)
        res_ele = etree.HTML(html_str)
        # print(type(res_ele),res_ele)
        wangzhan = '智联'
        # print(wangzhan)
        title= res_ele.xpath('//h1/text()')[0]
        # print(title)
        price = res_ele.xpath('//div[@class="l info-money"]/strong/text()')[0]
        # print(price)
        weizhi = res_ele.xpath('//div[@class="info-three l"]/span[1]/a/text()')[0]
        # print(weizhi)
        jingyan = res_ele.xpath('//div[@class="info-three l"]/span[2]/text()')[0]
        # print(jingyan)
        xueli = res_ele.xpath('//div[@class="info-three l"]/span[3]/text()')[0]
        # print(xueli)
        shijian = res_ele.xpath('//span[@class="company-right"]/span//text()')[0]
        # print(shijian)
        info = res_ele.xpath('//div[@class="pos-ul"]/p//text()')
        if info:
            info = ''.join(info[:2])
        # print(info)

        item = Zhilian()
        item['wangzhan'] = wangzhan
        item['title'] = title
        item['price'] = price
        item['weizhi'] = weizhi
        item['jingyan'] = jingyan
        item['xueli'] = xueli
        item['shijian'] = shijian
        item['info'] = info

        yield item

